/*
 * @Date: 2023-03-15 14:50:09
 * @LastEditTime: 2023-03-18 11:35:37
 * @FilePath: /hua-fei-chong-zhi/src/store/useUserinfoStore.ts
 * 介绍:
 */
import { defineStore } from "pinia";
import {
  apiBaseData,
  BaseData,
  apiUserinfo,
  Userinfo,
} from "@@/api/module/userinfo";

export default defineStore("StoreUserinfo", {
  //*全局仓库
  state: initState,
  //*全局函数
  actions: {
    async getBaseData() {
      const res = await apiBaseData();
      if (!res) return;
      this.$patch((state) => (state.baseData = res));
      return res;
    },
    async getUserinfo() {
      const res = await apiUserinfo();
      if (!res) return;
      this.$patch((state) => {
        if (res.level >= 0) state.is = { ...state.is, isVip: true };
        else state.is = { ...state.is, isVip: false };
        state.userinfo = res;
      });
    },
  },
  //*计算属性
  getters: {},
  //*仓库数据缓存
  persist: [
    {
      key: "StateUserinfo",
      storage: localStorage, //指定储存位置
    },
  ],
});
/**state类型定义 */
interface StoreUserinfo {
  baseData: BaseData.Res | undefined;
  userinfo: Userinfo.Res | undefined;
  is:
    | {
        isVip: boolean;
      }
    | undefined;
}
/**初始化pinia */
function initState(): StoreUserinfo {
  return {
    baseData: undefined,
    userinfo: undefined,
    is: undefined,
  };
}
